#include <iostream>

using namespace std;

const int N = 15;
int g[N][N];
int n, m;
bool vis[N][N];
const int dir[4][2] = {{0,  1},
                       {1,  0},
                       {0,  -1},
                       {-1, 0}};
int res = N * N, sum = 0;

bool check() {
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j)
            if (!vis[i][1] || !vis[1][j]) return false;

    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j)
            if (!vis[i][m] || !vis[n][j]) return false;

    return true;
}

void dfs(int x, int y, int step, int t) {
    if (t > sum / 2) return;
    if (t == sum / 2) {
        if (x == n && y == m) res = check() ? min(res, step) : res;
        else res = min(res, step);
        return;
    }

    for (int i = 0; i < 4; ++i) {
        int xx = x + dir[i][0], yy = y + dir[i][1];
        if (xx < 1 || xx > n || yy < 1 || yy > m || vis[xx][yy]) continue;

        vis[xx][yy] = true;
        dfs(xx, yy, step + 1, t + g[xx][yy]);
        vis[xx][yy] = false;
    }
}

int main() {
    cin >> m >> n;
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j) {
            cin >> g[i][j];
            sum += g[i][j];
        }

    vis[1][1] = true;
    dfs(1, 1, 1, g[1][1]);
    cout << (res == N * N ? 0 : res);
    return 0;
}